﻿Imports System.Net
Imports System.Security
Imports System.Security.Principal
Imports System.Net.Dns

Partial Public Class Alterarsenha
    Inherits Paginabase



#Region "variaveis globais"

    Dim util As New Util
    Dim paginavisivel As Boolean
#End Region

#Region "Sub e Funções"

   

    Public Sub alterar()
        Try
            Dim lista As New List(Of String)
            util.getusuarios(lista)
            Dim senha As String = lista.Item(6).ToString()
            pnl1.Visible = False
            If txtsenhaatual.Text = "" Then
                txtsenhaatual.Focus()
                Throw New Exception("Digite A Senha Atual")
            ElseIf txtnovasenha.Text = "" Then
                txtnovasenha.Focus()
                Throw New Exception("Digite a Nova Senha")

            ElseIf txtsenhaatual.Text.ToUpper <> senha Then

                txtsenhaatual.Focus()
                Throw New Exception("Senha Atual Não Confere")
            End If


            util.getusuarios(lista)
            ''  ITEM UM DA LISTA OBTERM O ID 
            sessaousuario.usuario.id = Convert.ToInt32(lista.Item(0).ToString())
            sessaousuario.usuario.idnivel = Convert.ToInt32(lista.Item(1).ToString())
            sessaousuario.usuario.idusuario = Convert.ToInt32(lista.Item(0).ToString())
            sessaousuario.usuario.nome = Convert.ToString(lista.Item(3).ToString())
            sessaousuario.usuario.email = Convert.ToString(lista.Item(4).ToString())


            sessaousuario.usuario.apelido = txtlogin.Text.ToUpper
            sessaousuario.usuario.senha = txtnovasenha.Text.ToUpper
            sessaousuario.usuario.dt_cadastro = Convert.ToDateTime(lista.Item(7).ToString())

            sessaousuario.usuario.ip = ip()
            dal.usuario.alterarsenha(sessaousuario.usuario)
            sessaousuario.Logs.id = sessaousuario.usuario.id



            Throw New Exception("Senha Alterada com Sucesso")
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub

    Public Function ip()
        Dim ip_ As String = String.Empty
        Dim h As IPHostEntry = GetHostEntry(GetHostName)
        If h.AddressList.Length = 4 Then
            ip_ = CType(h.AddressList.GetValue(2), IPAddress).ToString()
        End If
        If String.IsNullOrEmpty(ip_) Then
            ip_ = String.Empty

        End If
        Return ip_
      
    End Function

#End Region

#Region "Eventos Form"

   

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            paginavisivel = util.validarpagina(5)
            If Not paginavisivel Then
                FormsAuthentication.SignOut()
                Response.Redirect(ResolveUrl("~/Login.aspx"))
            End If

            Dim principal As IPrincipal
            principal = HttpContext.Current.User

            txtlogin.Text = principal.Identity.Name

        End If
    End Sub


    Protected Sub imgalterar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgalterar.Click


        alterar()

    End Sub
#End Region

End Class